home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / what's new / sample code / printing / scriptable print simpletext / threedmetafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-03  |  12.0 KB  |  378 lines

  1. /*
  2.     File:        ThreeDMetafile.c
  3.  
  4.     Contains:    3D viewer support for SimpleText
  5.  
  6.     Version:    SimpleText 1.4 or later
  7.  
  8. ** Copyright 1994-1996 Apple Computer. All rights reserved.
  9. **
  10. **    You may incorporate this sample code into your applications without
  11. **    restriction, though the sample code has been provided "AS IS" and the
  12. **    responsibility for its operation is 100% yours.  However, what you are
  13. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  14. **    after having made changes. If you're going to re-distribute the source,
  15. **    we require that you make it clear in the source that the code was
  16. **    descended from Apple Sample Code, but that you've made changes.
  17.  
  18. */
  19.  
  20.  
  21.  
  22. #include "MacIncludes.h"
  23. #include <String.h>
  24. #include "ThreeDMetafile.h"
  25.  
  26.  
  27. static GlobalEntry    gProcPointers[] =
  28.     {
  29.     {uppViewerLibNewProcInfo, nil},
  30.     {uppViewerLibDisposeProcInfo, nil},
  31.  
  32.     {uppViewerLibUseFileProcInfo, nil},
  33.     {uppViewerLibUseDataProcInfo, nil},
  34.     
  35.     {uppViewerLibWriteFileProcInfo, nil},
  36.     {uppViewerLibWriteDataProcInfo, nil},
  37.     
  38.     {uppViewerLibDrawProcInfo, nil},
  39.  
  40.     {uppViewerLibGetPictProcInfo, nil},
  41.     {uppViewerLibGetButtonRectProcInfo, nil},
  42.  
  43.     {uppViewerLibGetDimensionProcInfo, nil},
  44.  
  45.     {uppViewerLibSetFlagsProcInfo, nil},
  46.     {uppViewerLibGetFlagsProcInfo, nil},
  47.  
  48.     {uppViewerLibSetBoundsProcInfo, nil},
  49.     {uppViewerLibGetBoundsProcInfo, nil},
  50.  
  51.     {uppViewerLibSetPortProcInfo, nil},
  52.     {uppViewerLibGetPortProcInfo, nil},
  53.  
  54.     {uppViewerLibEventProcInfo, nil},
  55.     {uppViewerLibAdjustCursorProcInfo, nil},
  56.  
  57.     {uppViewerLibGetStateProcInfo, nil},
  58.  
  59.     {uppViewerLibClearProcInfo, nil},
  60.     {uppViewerLibCutProcInfo, nil},
  61.     {uppViewerLibCopyProcInfo, nil},
  62.     {uppViewerLibPasteProcInfo, nil}
  63.     };    
  64.     
  65.  
  66. // --------------------------------------------------------------------------------------------------------------
  67. // PRIVATE ROUTINES
  68. // --------------------------------------------------------------------------------------------------------------
  69. /* rather than include the whole StdCLib, I'll just define it here */
  70. static int strcmp(const char * str1, const char * str2)
  71. {
  72.     const    unsigned char* p1 = (unsigned char *) str1;
  73.     const    unsigned char* p2 = (unsigned char *) str2;
  74.     unsigned char        c1, c2;
  75.     
  76.     while ((c1 = *p1++) == (c2 = *p2++))
  77.         if (!c1)
  78.             return(0);
  79.  
  80.     return(c1 - c2);
  81. }
  82.  
  83. static UniversalProcPtr MakeARoutineDescriptor(void *theProc, ProcInfoType theProcInfo)
  84. /*
  85.     The Symantec compiler doesn't like jumping into staticly done routine descriptors,
  86.     so we have to actually allocate them as pointers.  Sigh.
  87. */
  88. {
  89.     RoutineDescriptor rd = BUILD_ROUTINE_DESCRIPTOR(0, nil);
  90.     
  91.     Ptr    newPtr;
  92.     
  93.     rd.routineRecords[0].procInfo = theProcInfo;
  94.     rd.routineRecords[0].procDescriptor = theProc;
  95.     rd.routineRecords[0].ISA = kPowerPCISA + kPowerPCRTA;
  96.     newPtr = NewPtrClear(sizeof(rd));
  97.     if (newPtr)
  98.         BlockMove(&rd, newPtr, sizeof(rd));
  99.     return((UniversalProcPtr) newPtr);
  100.     
  101. } // MakeARoutineDescriptor
  102.  
  103. // --------------------------------------------------------------------------------------------------------------
  104. static void FillInEntryPoints(void)
  105. {
  106.     long                result;
  107.     
  108.     if ( 
  109.         (Gestalt('qd3d', &result) == noErr) && 
  110.         (Gestalt('sysa', &result) == noErr) && (result == 2) 
  111.         )
  112.         {
  113.         CFragSymbolClass    symClass;
  114.         CFragConnectionID    connID;
  115.         Ptr                 mainAddr;
  116.         Str255                errName;
  117.         long                procID = 0;
  118.         
  119.         if (GetSharedLibrary("\pQD3DViewerLib", 'pwpc', 1, &connID, &mainAddr, errName) == noErr)
  120.             {
  121.             FindSymbol(connID, "\pQ3ViewerNew", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  122.             FindSymbol(connID, "\pQ3ViewerDispose", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  123.             
  124.             FindSymbol(connID, "\pQ3ViewerUseFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  125.             FindSymbol(connID, "\pQ3ViewerUseData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  126.  
  127.             FindSymbol(connID, "\pQ3ViewerWriteFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  128.             FindSymbol(connID, "\pQ3ViewerWriteData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  129.  
  130.             FindSymbol(connID, "\pQ3ViewerDraw", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  131.  
  132.             FindSymbol(connID, "\pQ3ViewerGetPict", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  133.             FindSymbol(connID, "\pQ3ViewerGetButtonRect", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  134.  
  135.             FindSymbol(connID, "\pQ3ViewerGetDimension", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  136.  
  137.             FindSymbol(connID, "\pQ3ViewerSetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  138.             FindSymbol(connID, "\pQ3ViewerGetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  139.  
  140.             FindSymbol(connID, "\pQ3ViewerSetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  141.             FindSymbol(connID, "\pQ3ViewerGetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  142.  
  143.             FindSymbol(connID, "\pQ3ViewerSetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  144.             FindSymbol(connID, "\pQ3ViewerGetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  145.  
  146.             FindSymbol(connID, "\pQ3ViewerEvent", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  147.             FindSymbol(connID, "\pQ3ViewerAdjustCursor", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  148.  
  149.             FindSymbol(connID, "\pQ3ViewerGetState", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  150.  
  151.             FindSymbol(connID, "\pQ3ViewerClear", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  152.             FindSymbol(connID, "\pQ3ViewerCut", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  153.             FindSymbol(connID, "\pQ3ViewerCopy", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  154.             FindSymbol(connID, "\pQ3ViewerPaste", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  155.             }
  156.         
  157.         // build the routine descriptors
  158.         {
  159.         short    i;
  160.         
  161.         for (i = 0; i < procID; ++i)
  162.             gProcPointers[i].entry = MakeARoutineDescriptor(gProcPointers[i].entry, gProcPointers[i].info);
  163.         }
  164.         
  165.         // as long as it isn't NIL, we're OK
  166.         if (gProcPointers[0].entry)
  167.             gMachineInfo.haveThreeD = true;
  168.         }
  169.  
  170. } // FillInEntryPoints
  171.  
  172. // --------------------------------------------------------------------------------------------------------------
  173. // OOP INTERFACE ROUTINES
  174. // --------------------------------------------------------------------------------------------------------------
  175.  
  176. static OSErr ThreeDCloseWindow(
  177.             WindowRef pWindow, 
  178.             WindowDataPtr pData)
  179. {
  180. #pragma unused (pWindow)
  181.  
  182.  
  183.     ViewerLib_Dispose( ((ThreeDDataPtr)pData)->viewerObject);
  184.  
  185.     return(noErr);
  186.     
  187. } // ThreeDCloseWindow
  188.  
  189. // --------------------------------------------------------------------------------------------------------------
  190. static OSErr ThreeDGetBalloon(
  191.             WindowRef pWindow, 
  192.             WindowDataPtr pData, 
  193.             Point *localMouse,
  194.             short * returnedBalloonIndex, 
  195.             Rect *returnedRectangle)
  196. {
  197. #pragma unused (pWindow, pData, localMouse, returnedBalloonIndex, returnedRectangle)
  198.  
  199.     *returnedBalloonIndex = iDidTheBalloon;
  200.     
  201.     return(noErr);
  202.     
  203. } // ThreeDGetBalloon
  204.  
  205. // --------------------------------------------------------------------------------------------------------------
  206. static Boolean    ThreeDFilterEvent(
  207.             WindowRef pWindow, 
  208.             WindowDataPtr pData, 
  209.             EventRecord *pEvent)
  210. {
  211. #pragma unused (pWindow)
  212.     
  213.     if (pEvent->what == activateEvt)
  214.         {
  215.         if (pEvent->modifiers & activeFlag)
  216.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) | kQ3ViewerActive);
  217.         else
  218.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) & ~kQ3ViewerActive);
  219.         }
  220.         
  221.     return((Boolean)ViewerLib_Event (((ThreeDDataPtr)pData)->viewerObject, pEvent));
  222.     
  223. } // ThreeDFilterEvent
  224.  
  225.  
  226. // --------------------------------------------------------------------------------------------------------------
  227. static OSErr ThreeDAdjustCursor(WindowRef pWindow, WindowDataPtr pData, Point *localMouse, Rect *globalRect)
  228. {
  229. #pragma unused (pWindow, globalRect)
  230.  
  231.     OSErr anErr = noErr;
  232.  
  233.     ViewerLib_AdjustCursor(((ThreeDDataPtr)pData)->viewerObject, localMouse);
  234.     
  235.     return(eActionAlreadyHandled);
  236.     
  237. } // ThreeDAdjustCursor
  238.  
  239. // --------------------------------------------------------------------------------------------------------------
  240. static OSErr ThreeDGetCoachRectangle(WindowRef pWindow, WindowDataPtr pData, Rect *pRect, Ptr name)
  241. {
  242. #pragma unused (pWindow)
  243.  
  244.     OSErr anErr = fnfErr;
  245.     
  246.     if (strcmp(name, "Camera") == 0)
  247.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonCamera, pRect);
  248.     if (strcmp(name, "Truck") == 0)
  249.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonTruck, pRect);
  250.     if (strcmp(name, "Orbit") == 0)
  251.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonOrbit, pRect);
  252.     if (strcmp(name, "Zoom") == 0)
  253.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonZoom, pRect);
  254.     if (strcmp(name, "Dolly") == 0)
  255.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonDolly, pRect);
  256.             
  257.     return(anErr);
  258.     
  259. } // ThreeDGetCoachRectangle
  260.  
  261. // --------------------------------------------------------------------------------------------------------------
  262. void ThreeDGetFileTypes(
  263.         OSType * pFileTypes,
  264.         OSType * pDocumentTypes,
  265.         short * numTypes)
  266. {
  267.     if (!gMachineInfo.haveThreeD)
  268.         FillInEntryPoints();
  269.         
  270.     if (gMachineInfo.haveThreeD)
  271.         {
  272.         pFileTypes[*numTypes]         = '3DMF';
  273.         pDocumentTypes[*numTypes]     = kThreeDWindow;
  274.         (*numTypes)++;
  275.         }
  276.     
  277. } // ThreeDGetFileTypes
  278.  
  279. // --------------------------------------------------------------------------------------------------------------
  280. static OSErr    ThreeDUpdateWindow(WindowRef pWindow, WindowDataPtr pData)
  281. {
  282. #pragma unused (pWindow)
  283.  
  284.     
  285.     ViewerLib_Draw(((ThreeDDataPtr)pData)->viewerObject);
  286.     
  287.     return(noErr);
  288.     
  289. } // ThreeDUpdateWindow
  290.  
  291. // --------------------------------------------------------------------------------------------------------------
  292. static OSErr    ThreeDAdjustMenus(WindowRef pWindow, WindowDataPtr pData)
  293. {
  294. #pragma unused (pWindow)
  295.  
  296.     OSErr anErr = noErr;
  297.     
  298.     if (ViewerLib_GetState(((ThreeDDataPtr)pData)->viewerObject) ) 
  299.         EnableCommand(cCopy);
  300.  
  301.     return(anErr);
  302.     
  303. } // ThreeDAdjustMenus
  304.  
  305. // --------------------------------------------------------------------------------------------------------------
  306. static OSErr    ThreeDCommand(WindowRef pWindow, WindowDataPtr pData, short commandID, long menuResult)
  307. {
  308. #pragma unused (menuResult)
  309.  
  310.     OSErr                        anErr = noErr;
  311.     ViewerObject                 theViewer = ((ThreeDDataPtr)pData)->viewerObject;
  312.     
  313.     SetPort((GrafPtr) GetWindowPort(pWindow));
  314.         
  315.     switch (commandID) 
  316.         {
  317.         case cCopy:
  318.             anErr = ViewerLib_Copy(theViewer);
  319.             if (anErr == noErr)
  320.                 anErr = eActionAlreadyHandled;
  321.             break;
  322.         } // switch (commandID)
  323.         
  324.     return(anErr);
  325.     
  326. } // ThreeDCommand
  327.  
  328. // --------------------------------------------------------------------------------------------------------------
  329. static OSErr    ThreeDMakeWindow(
  330.             WindowRef pWindow,
  331.             WindowDataPtr pData)
  332. {
  333.     OSErr                        anErr = noErr;
  334.     ViewerObject                viewerObj;
  335.  
  336.     pData->pCloseWindow         = (CloseWindowProc)            ThreeDCloseWindow;
  337.     pData->pFilterEvent         = (FilterEventProc)            ThreeDFilterEvent;
  338.     pData->pGetBalloon             = (GetBalloonProc)            ThreeDGetBalloon;
  339.     pData->pUpdateWindow         = (UpdateWindowProc)        ThreeDUpdateWindow;
  340.     pData->pAdjustMenus         = (AdjustMenusProc)            ThreeDAdjustMenus;
  341.     pData->pCommand                 = (CommandProc)                ThreeDCommand;
  342.     pData->pAdjustCursor         = (AdjustCursorProc)        ThreeDAdjustCursor;
  343.     pData->pGetCoachRectangle    = (GetCoachRectangleProc)    ThreeDGetCoachRectangle;
  344.  
  345.     pData->dragWindowAligned    = 0;
  346.  
  347.     viewerObj = ViewerLib_New((CGrafPtr )pWindow,  &pData->contentRect,  kQ3ViewerDefault | kQ3ViewerDraggingOff); 
  348.  
  349.     if (viewerObj == 0)
  350.         {
  351.         anErr = memFullErr;
  352.         }
  353.     else
  354.         {
  355.         anErr = ViewerLib_UseFile(viewerObj, pData->dataRefNum);
  356.         if (anErr == noErr)
  357.             ((ThreeDDataPtr)pData)->viewerObject = viewerObj;
  358.         else
  359.             ViewerLib_Dispose(viewerObj);
  360.         
  361.         }
  362.     
  363.     return(anErr);
  364.     
  365. } // ThreeDMakeWindow
  366.  
  367. // --------------------------------------------------------------------------------------------------------------
  368. OSErr    ThreeDPreflightWindow(PreflightPtr pPreflightData)
  369. {    
  370.     pPreflightData->continueWithOpen     = true;
  371.     pPreflightData->makeProcPtr         = ThreeDMakeWindow;
  372.     pPreflightData->resourceID            = kThreeDWindowID;
  373.     pPreflightData->storageSize         = sizeof(ThreeDDataRecord);
  374.  
  375.     return(noErr);
  376.     
  377. } // ThreeDPreflightWindow
  378.